#include <__cross_studio_io.h>
#include <msp430x14x.h>
#include <math.h>
#include <string.h>

extern void bcdadd(long int *test,long int *tes);
typedef struct 
{
  signed long int value;
}bcd32_t;

long unsigned int combine(int i)
{
  switch (i)
  {
    case 0:
      return 1;
    case 1:
      return 16;
    case 2:
      return 256;
    case 3:
      return 4096;
    case 4:
      return 65536;
    case 5:
      return 1048576;
    case 6:
      return 16777216;
    case 7:
      return 268435456;
  }
}

long unsigned int bcd(long signed int dec)
{
        long unsigned int op;
	long unsigned int result=0;
        long unsigned int sign=0;
        int i;
        if (dec>0)
        op=dec;
        else
        {
         op=dec*(-1); 
         sign=combine(7);
        }
        for (i=6;i>=0;i--)
        {
            long int temp=op/(long int)(pow(10,i));            
            op-=temp*(long int)(pow(10,i));
            result=result+temp*combine(i);          
        }
        result=result+sign;
        return result;
}

long signed int unbcd(long unsigned int bcd)
{
        long signed int op;
	long signed int result=0;
        signed int i;
        int sign=1;
        if (bcd<268435456)
        op=bcd;
        else
        {
         //op=bcd*(-1); 
         op=bcd-268435456;
         sign=-1;
        }
        debug_printf("OP: %ld\n",op);
        for (i=6;i>=0;i--)
        {
            long signed int temp=op/combine(i);            
            op-=temp*combine(i);
            result=result+temp*(long int)(pow(10,i));          
            debug_printf("temp: %ld\n", temp);
            debug_printf("result: %ld\n", result);
        }
        //result*=-1;
        //debug_printf("final result: %ld\n", result);
        return result*sign;
}

void babbage(unsigned int PolyOrder, unsigned int NumItems, bcd32_t* Elements)
{
  //create a 2D table
  signed long int table [7][10];  
  int i,j,k;
  //clear the table of existing contents
  for (i=0;i<7;i++)
  {
    for (j=0;j<10;j++)
      table[i][j]=0;
  }
  
  //store the known solutions to first row
  for (i=0;i<NumItems; i++)
    {
      bcd32_t temp=*Elements;
      table[0][i]=temp.value;
      Elements++;
    }
 
  //now, determine the minimum difference
  k=PolyOrder;
  for (i=0; i<PolyOrder+1; i++)
  {
    for (j=0; j<k; j++)
    {
      table[i+1][j]=table[i][j+1]-table[i][j];
    }
    k--;
  }
  
  //populate the last row
  for (i=1; i<NumItems; i++)
  {
    table[PolyOrder][i]=table[PolyOrder][0];
  }

  //populate all remaining rows
  for (i=PolyOrder-1; i>=0; i--)
  {  
    k=2;
    for (j=k; j<NumItems; j++)
    {
      table[i][j]=table[i][j-1]+table[i+1][j-1];
    }
  }

  


    for (i=0;i<7;i++)
  {
    for (j=0;j<10;j++)
      debug_printf("%d, ",table[i][j]);
    debug_printf("\n");
  }

  debug_printf("\n");
  
}

void main(void)
{
  long int *point,*point2;
  long int op1,op2;
  long int hi;
  char *hex = "12";
  char *end;
  unsigned long int x;
  signed long int y;
  int i=0;
  bcd32_t results[7];
  bcd32_t *ap;

  //op1=117;
  //point=&op1;
  //op2=53;
  //point2=&op2;

  results[0].value=1;
  results[1].value=4;
  results[2].value=15;
  results[3].value=40;
  

  ap=&results[0];
  //babbage(3,7,ap);

  x=bcd(-3828182);
  y=unbcd(x);
  debug_printf("%ld\n",x);
  debug_printf("%ld\n",y);
  debug_printf("yo");

  

}
